package com.ruoyi.program.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.program.domain.WxQuestionBank;
import com.ruoyi.program.service.IWxQuestionBankService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题库管理Controller
 *
 * @author ruoyi
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/program/bank")
public class WxQuestionBankController extends BaseController
{
    @Autowired
    private IWxQuestionBankService wxQuestionBankService;

    /**
     * 查询题库管理列表
     */
    @PreAuthorize("@ss.hasPermi('program:bank:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxQuestionBank wxQuestionBank)
    {
        startPage();
        List<WxQuestionBank> list = wxQuestionBankService.selectWxQuestionBankList(wxQuestionBank);
        return getDataTable(list);
    }

    /**
     * 导出题库管理列表
     */
    @PreAuthorize("@ss.hasPermi('program:bank:export')")
    @Log(title = "题库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxQuestionBank wxQuestionBank)
    {
        List<WxQuestionBank> list = wxQuestionBankService.selectWxQuestionBankList(wxQuestionBank);
        ExcelUtil<WxQuestionBank> util = new ExcelUtil<WxQuestionBank>(WxQuestionBank.class);
        util.exportExcel(response, list, "题库管理数据");
    }

    /**
     * 获取题库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('program:bank:query')")
    @GetMapping(value = "/{bankId}")
    public AjaxResult getInfo(@PathVariable("bankId") Long bankId)
    {
        return success(wxQuestionBankService.selectWxQuestionBankByBankId(bankId));
    }

    /**
     * 新增题库管理
     */
    @PreAuthorize("@ss.hasPermi('program:bank:add')")
    @Log(title = "题库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxQuestionBank wxQuestionBank)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();

        // 为公共字段赋值
        wxQuestionBank.setCreateBy(username);  // 创建人
        wxQuestionBank.setUpdateBy(username);  // 更新人（）
        wxQuestionBank.setCreateTime(new Date());  // 创建时间（可新增时与创建人一致选，BaseEntity可能默认自动填充）
        wxQuestionBank.setUpdateTime(new Date());  // 更新时间

        return toAjax(wxQuestionBankService.insertWxQuestionBank(wxQuestionBank));
    }

    /**
     * 修改题库管理
     */
    @PreAuthorize("@ss.hasPermi('program:bank:edit')")
    @Log(title = "题库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxQuestionBank wxQuestionBank)
    {
        return toAjax(wxQuestionBankService.updateWxQuestionBank(wxQuestionBank));
    }

    /**
     * 删除题库管理
     */
    @PreAuthorize("@ss.hasPermi('program:bank:remove')")
    @Log(title = "题库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bankIds}")
    public AjaxResult remove(@PathVariable Long[] bankIds)
    {
        return toAjax(wxQuestionBankService.deleteWxQuestionBankByBankIds(bankIds));
    }
}
